<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Entity\Result;
use App\Model\Device;
use App\Model\DevicePublish;
use App\Service\DeviceMqttService;
use App\Service\MqttClientService;
use App\Utils\Helper;
use App\Utils\Logger;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;
use Simps\MQTT\Client;
use Swoole\Coroutine;


/**
 * Class IndexController
 *
 * @package App\Controller
 * @Controller()
 */
class IndexOldController extends AbstractController
{

    /**
     * @Inject
     * @var DeviceMqttService
     */
    protected DeviceMqttService $deviceMqttService;

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function statusQuery()
    {
        $mac = $this->request->input('mac');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }
        if ($device->clientId) {
            # 下发查询命令
            $cmd = $device->mac . "0C";
            $this->sendCmd($device->id, $device->clientId, $cmd);
            if($device->user && $user->type == 2){
                Helper::usedAdd($user->id,$mac);
            }
        }

        return Result::success();
    }

    # 拉闸
    public function stop()
    {
        $mac = $this->request->input('mac');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发拉闸命令
                            $data = "0D" . "02";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;
            } else {
                # 下发拉闸命令
                $cmd = $device->mac . "0D" . "02";
                $this->sendCmd($device->id, $device->clientId, $cmd);
                // sleep(1);
                # 查询
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
            if($device->user && $user->type == 2){
                Helper::usedAdd($user->id,$mac);
            }
        }

        return Result::success();
    }

    # 合闸
    public function start()
    {
        $mac = $this->request->input('mac');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }
        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发合闸命令
                            $data = "0D" . "01";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;

            } else {
                # 下发合闸命令
                $cmd = $device->mac . "0D" . "01";
                $this->sendCmd($device->id, $device->clientId, $cmd);

                # 查询
                // sleep(1);
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }

    # 重启
    public function restart()
    {
        $mac = $this->request->input('mac');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发合闸命令
                            $data = "03";
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 2);
                            $isSend = true;

            } else {
                # 下发合闸命令
                $cmd = $device->mac . "03" ;
                $this->sendCmd($device->id, $device->clientId, $cmd);

                # 查询
                // sleep(1);
                Coroutine::sleep(1);
                $cmd = $device->mac . "0C";
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }

        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }
    
    
    
    # 修改电量
    public function editDu()
    {
        $mac = $this->request->input('mac');
        $du = (int) $this->request->input('du');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发充值命令
                            $du = strtoupper(dechex($du));
                            $data = "0A" . "02" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                            $isSend = true;

            } else {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $cmd = $device->mac . "0A" . "02" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                # 查询
                //$this->sendCmd($device->id, $device->clientId, $device->mac . "0C");
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }
    

    # 充电
    public function recharge()
    {
        $mac = $this->request->input('mac');
        $du = (int) $this->request->input('du');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {

                $cache = $this->container->get(\Psr\SimpleCache\CacheInterface::class);
                go(function () use ($mac, $device, $cache, $du) {
                            # 下发充值命令
                            $du = strtoupper(dechex($du));
                            $data = "0A" . "01" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                });

            } else {
                # 下发充值命令
                $du = strtoupper(dechex($du));
                $cmd = $device->mac . "0A" . "01" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd, 0);
                # 查询
                //$this->sendCmd($device->id, $device->clientId, $device->mac . "0C");
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }

    # 清0
    public function clear()
    {
        $mac = $this->request->input('mac');
        $type = $this->request->input('type') ?? 1;
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发清 0 命令
                            $du = strtoupper(dechex(0));
                            if ($type == 2) {
                                $du = strtoupper(dechex(1000));
                            }
                            $data = "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
            } else {
                # 下发清0命令
                $du = strtoupper(dechex(0));
                if ($type == 2) {
                    $du = strtoupper(dechex(1000));
                }
                $cmd = $device->mac . "0A" . "03" . $this->deviceMqttService->toLen($du, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }

    # 调表
    public function speed()
    {
        $mac = $this->request->input('mac');
        $speed = $this->request->input('speed');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }
        if ($device->clientId) {
             if ($device->mMac) {
                            # 下发调表
                            $speed = strtoupper(dechex($speed));
                            $data = "02" . "03" . $this->deviceMqttService->toLen($speed, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($speed, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
            } else {
                # 下发调表
                $speed = strtoupper(dechex($speed));
                $cmd = $device->mac . "02" . "03" . $this->deviceMqttService->toLen($speed, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($speed, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }
    
    
    
   public function curr()
    {
        $mac = $this->request->input('mac');
        $warning_curr = $this->request->input('warning_curr');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }
        if ($device->clientId) {
             if ($device->mMac) {
                            # 下发调表
                            $warning_curr = strtoupper(dechex($warning_curr));
                            $data = "02" . "06" . $this->deviceMqttService->toLen($warning_curr, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($warning_curr, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
            } else {
                # 下发调表
                $warning_curr = strtoupper(dechex($warning_curr));
                $cmd = $device->mac . "02" . "06" . $this->deviceMqttService->toLen($warning_curr, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($warning_curr, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }



//修改计量模式
    public function setMode()
    {
        $mac = $this->request->input('mac');
        $type = $this->request->input('type');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }
        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发模式
                            $type = strtoupper(dechex($type));
                            $data = "02" . "02" . $this->deviceMqttService->toLen($type, 2);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            # 保存参数
                            $data = "02" . "FE" . $this->deviceMqttService->toLen($type, 2);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
            } else {
                # 下发模式
                $type = strtoupper(dechex($type));
                $cmd = $device->mac . "02" . "02" . $this->deviceMqttService->toLen($type, 2);
                $this->sendCmd($device->id, $device->clientId, $cmd);
                # 保存
                $cmd = $device->mac . "02" . "FE" . $this->deviceMqttService->toLen($type, 2);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }
        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }


    public function querySpeed()
    {
        $mac = $this->request->input('mac');
        $token = $this->request->input('token');
        if(!$token){
            return Result::error(1001,'密钥无效');
        }
        $device = Device::where('mac', $mac)->with('user')->first();
        $user = \App\Model\User::where('token',$token)->first();
        if(!$user){
            return Result::error(1001,'用户不存在');
        }
        if($user->type == 2 && $user->call_count <= $user->call_count_used){
            return Result::error(1001,'用户次数已用尽');
        }

        if ($device->clientId) {
            if ($device->mMac) {
                            # 下发查表
                            $data = "02" . "83" . $this->deviceMqttService->toLen(0, 8);
                            $cmd = $device->mac . "10" . "03" . $this->deviceMqttService->toLen(strlen($data) / 2, 2) . $data;
                            $this->sendCmd($device->id, $device->clientId, $cmd);
                            $isSend = true;
            } else {
                # 下发调表
                $cmd = $device->mac . "02" . "83" . $this->deviceMqttService->toLen(0, 8);
                $this->sendCmd($device->id, $device->clientId, $cmd);
            }
        }

        if($device->user && $user->type == 2){
            Helper::usedAdd($user->id,$mac);
        }
        return Result::success();
    }

   public function sendCmd1($deviceId, $topic, $cmd, $qos = 1)
    {
        DevicePublish::create([
            'device_id' => $deviceId,
            'topic' => $topic,
            'data' => $this->deviceMqttService->returnData1($cmd),
            'qos' => $qos
        ]);
    }

    public function sendCmd($deviceId, $topic, $cmd, $qos = 1)
    {
        DevicePublish::create([
            'device_id' => $deviceId,
            'topic' => $topic,
            'data' => $this->deviceMqttService->returnData($cmd),
            'qos' => $qos
        ]);
    }
}
